package com.sny.upgrade.ui;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.sny.upgrade.AppUpgradeImple;
import com.sny.upgrade.R;
import com.sny.upgrade.util.SPUtils;
import com.sny.upgrade.util.UpgradeUtil;

/**
 * @author wangying
 */
public class DialogShow implements Runnable {

    private UpdateConfirmDialog mDialog;
    private Context mContext;
    private Activity mHomeActivity;

    public DialogShow(UpdateConfirmDialog dialog, Context context, Activity homeActivity) {
        mDialog = dialog;
        mContext = context;
        mHomeActivity = homeActivity;
    }

    @Override
    public void run() {

        String message = FirebaseRemoteConfig.getInstance().getString("cur_version_message");

        mDialog.setHeight((int) mContext.getResources().getDimension(
                R.dimen.dialog_update_height));
        mDialog.setTitleText(R.string.update_update);
        mDialog.setOkText(R.string.update_update);
        // message 换行用 &来表示，因为直接用\n会不起作用。 TODO
        message = message.replace("&", "\n");
        mDialog.setContentText(message);
        mDialog.setOnConfirmDetailListener(new ConfirmCommonDialog.OnConfirmDetailListener() {
            @Override
            public void onConfirm() {
                Log.i("upgrade", "onConfirm");
                UpgradeUtil.forwardToGp(mContext);
            }

            @Override
            public void onCancel() {
                Log.i("upgrade", "onCancel");
            }

            @Override
            public void onBackPress() {
                Log.i("upgrade", "onBackPress");
            }
        });

        if (!mHomeActivity.isFinishing()) {
            mDialog.showDialog();
            long start = System.currentTimeMillis();
            SPUtils.put(mContext, AppUpgradeImple.LAST_NOTIFY_TIME, System.currentTimeMillis());
            Log.i("upgrade", "cost" + (System.currentTimeMillis() - start));
        }
    }

}
